package com.smartwasser.yunhtgl.apphtgl.network;

import android.util.Log;

import com.smartwasser.yunhtgl.apphtgl.App;
import com.smartwasser.yunhtgl.apphtgl.base.BaseConstant;
import com.smartwasser.yunhtgl.apphtgl.utils.Logger;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class Retrofits {
    //    public static String base = "http://119.57.140.132:8615";//公司测试
//    public static String base = "http://10.2.100.144:8080";//集团内网
        public static String base = "http://10.2.100.143:8081";//集团测试
//    public static String base = "http://39.106.43.224:8080";//阿里云服务器测试
    public static String baseUrlJt = base + "/jthtgl/";
    public static String baseUrlShj = base + "/shjhtgl/";
    public static String baseUrlFir = "http://api.fir.im/apps/latest/";
    private static Retrofits mRetrofits;
    private Retrofit mRetrofit;

    private Retrofits() {
        initRetrofit();
    }

    public static synchronized Retrofits getInstance() {
        if (mRetrofits == null) {
            mRetrofits = new Retrofits();
        }
        return mRetrofits;
    }

    public static void clean() {
        if (mRetrofits != null) {
            mRetrofits = null;
        }
    }

    private void initRetrofit() {
        HttpLoggingInterceptor LoginInterceptor = new HttpLoggingInterceptor(new HttpLoggingInterceptor.Logger() {
            @Override
            public void log(String message) {
                Log.i("RetrofitLog","retrofitBack ======================= "+message);
            }
        });
//        HttpLoggingInterceptor LoginInterceptor = new HttpLoggingInterceptor();
        if (BaseConstant.LOG) {
            LoginInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        } else {
            LoginInterceptor.setLevel(HttpLoggingInterceptor.Level.NONE);
        }
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        if (true) {
            builder.addInterceptor(LoginInterceptor);
        }
        builder.connectTimeout(15, TimeUnit.SECONDS);
        builder.readTimeout(20, TimeUnit.SECONDS);
        builder.writeTimeout(20, TimeUnit.SECONDS);
        builder.retryOnConnectionFailure(true);
        OkHttpClient client = builder.build();
        if ("1".equals(App.getInstance().getSystem())) {
            Logger.e("获取水环境数据");
            mRetrofit = new Retrofit.Builder().baseUrl(Retrofits.baseUrlShj)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .client(client).build();
        }else if ("2".equals(App.getInstance().getSystem())){
            Logger.e("获取版本");
            mRetrofit = new Retrofit.Builder().baseUrl(Retrofits.baseUrlFir)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .client(client).build();

        } else {
            Logger.e("获取集团数据");
            mRetrofit = new Retrofit.Builder().baseUrl(Retrofits.baseUrlJt)
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                    .client(client).build();
        }

    }

    public <T> T createReq(Class<T> reqServer) {
        return mRetrofit.create(reqServer);
    }
}
